package com.config;

import com.sun.org.apache.xpath.internal.operations.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
public class SecurityConfig2 extends WebSecurityConfigurerAdapter {
    //注入userDetailssevice
    @Autowired
    UserDetailsService userDetailsService;
    //注入数据源
    @Autowired
    private DataSource dataSource;

    //配置数据源
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
//        //设置自动创建表
//        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }

    ;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //通过userDetailsservice进行配置用户名、密码、权限。
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Bean
    PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //设置退出界面路径（随意）、退出成功后返回路径
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/home").permitAll();
        http.exceptionHandling().accessDeniedPage("/unpage.html");
        http.formLogin()//自定义登录页面
                .loginPage("/login.html") //设置登陆界面
                .loginProcessingUrl("/user/login")//访问路径
                .defaultSuccessUrl("/success.html")//登录成功跳转路径
                .and().authorizeRequests()
                .antMatchers("/*", "/test/hello", "/user/login").permitAll()//设置那些路径可以直接访问
                //当前登录用户，拥有admin权限才能访问这个路径
//                .antMatchers("/test/index").hasAnyRole("sale","admin")
                //1   .antMatchers("/test/index").hasAnyAuthority("admin","roles")
//             2   .antMatchers("/test/index").hasAnyAuthority("admin")
                //3       .antMatchers("/test/index").hasRole("sale")
                .anyRequest().authenticated()
                //调用rememberMe服务验证服务器token  参数方法为数据库配置方法
                .and().rememberMe().tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(120)//设置token有效时长
                .userDetailsService(userDetailsService)
                        .and().csrf().disable();//关闭csrf防护
    }

}

